﻿using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Win32;

namespace VSTweaker
{
    public class SettingsPage : INotifyPropertyChanged
    {
        private RegistryKey _key;

        public SettingsPage(RegistryKey key)
        {
            _key = key;
        }

        public string Name
        {
            get
            {
                return _key.GetKeyName();
            }
        }

        public bool Enabled
        {
            get
            {
                var val = _key.GetValue("NoShowAllView");
                if (val == null) return true;

                return ((int)val) == 1;
            }
            set
            {
                _key.SetValue("NoShowAllView", value ? 0 : 1);
                OnPropertyChaned("Enabled");
            }
        }

        public IEnumerable<SettingsPage> Children
        {
            get
            {
                var subKeyNames = _key.GetSubKeyNames();
                foreach (var subkeyName in subKeyNames)
                {
                    yield return new SettingsPage(_key.OpenSubKey(subkeyName));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChaned(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
